import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

df = pd.read_csv("D:/wmq/xwechat_files/wxid_ggovepgejzk922_e65c/msg/file/2025-10/energy_utilization_daily.csv")

color_vals = df["oee_pct"].values

X = df.values
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

pca = PCA(n_components=2)
pcs = pca.fit_transform(X_scaled)
var_ratio = pca.explained_variance_ratio_

loadings = pd.DataFrame(
    pca.components_.T,
    index=df.columns,
    columns=["PC1", "PC2"]
)

print("Explained variance ratio:", var_ratio)
print("\nLoadings (Energy):\n", loadings.round(3))

plt.figure(figsize=(7,5))
plt.scatter(pcs[:,0], pcs[:,1], c=color_vals, s=40)
plt.title(f"PCA - Energy & Utilization | PC1 {var_ratio[0]*100:.1f}%, PC2 {var_ratio[1]*100:.1f}%")
plt.xlabel("PC1")
plt.ylabel("PC2")
plt.grid(True)
plt.colorbar(label="oee_pct")
plt.show()
